/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import midlet.MidletPlay;
import util.RecordHighScore;

/**
 * @author LTTDNQ
 */
public class MIDPCanvasEnd extends GameCanvas implements CommandListener{
    private MidletPlay _midlet;
    private Graphics _g;
    private Image _imgBg;
    private int _chieudai = getWidth(), _chieucao = getHeight();
    private int _score;
    private Command _cmExit;
    private RecordHighScore rhs;
    /**
     * constructor
     */
    public MIDPCanvasEnd(MidletPlay midlet, int xScore) {
        super(false);
        this._midlet = midlet; 
        rhs = RecordHighScore.getInstance();
        _g = this.getGraphics();
        _score = xScore;
         try {
              _cmExit =  new Command("Exit", Command.EXIT, 1);
            // Set up this canvas to listen to command events
            setCommandListener(this);
            // Add the Exit command
            
            addCommand(_cmExit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * paint
     */
    public void paintEnd() {
        try {
            _imgBg = Image.createImage("/Images/imgBackground.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        _g.drawImage(_imgBg, 0, 0, Graphics.LEFT | Graphics.TOP);
        Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
        _g.setColor(255,0,0);
        _g.setFont(font);
        _g.drawString("YOUR SCORE", _chieudai*3/10, _chieucao*4/10, Graphics.LEFT|Graphics.TOP);
        _g.drawString(String.valueOf(_score).toString(), _chieudai*5/10-2, _chieucao*5/10,  Graphics.LEFT|Graphics.TOP);
        
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {
    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
         if(command == _cmExit){
             rhs.addScore(_score);
             rhs.printScores();
            _midlet.vaoManHinhMenu();
        }
    }
}
